/**
 * 여행지 정보 입력 script
 */
var xhr; 
	function createXMLHttpRequest(){
		if(window.ActiveXObject){// ms explorer일 경우 
			xhr=new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			xhr=new XMLHttpRequest();
		}
	}
	//0. 지역 정보 입력값을 서버로 보내준다.
	function startInputTravelZone(){
		createXMLHttpRequest();
		xhr.onreadystatechange = callbackInputTravelZone;
		xhr.open("POST","/wetogether/travelinfo_input.do",true);
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UFT-8");
		var location = document.getElementById("location").value;
		var locationid = document.getElementById("locationid").value;
		var zoneinfo = document.getElementById("zoneinfo").value;
		var zoneimg = document.getElementById("zoneimg").value;
		xhr.send("mode=inputTravelZone&location="+location
				+"&locationid="+locationid+"&zoneinfo="
				+zoneinfo+"&zoneimg="+zoneimg);
	}
	function callbackInputTravelZone(){
		if(xhr.readyState==4){
			if(xhr.status==200){
				alert(xhr.responseText);
				clearInputTravelZone();
			}
		}
	}
	function clearInputTravelZone(){
		document.getElementById("locationid").value="";
		document.getElementById("location").value="";
		document.getElementById("zoneinfo").value="";
		document.getElementById("zoneimg").value="";
	}
	// 여행지 정보 모두 가져오기
	function startOutputTravelInfo(place){
		var locationId=document.getElementById("locationId").value;
		location.href="travelinfo_output.do?mode=outputTravelInfo&place="+place+"&locationId="+locationId;
	}
	//1. 여행지 정보 입력값을 서버로 보내준다.
	function startInputTravelInfo(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackInputTravelInfo; 
		xhr.open("POST","/wetogether/travelinfo_input.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");
		var locationid=checkedRadioButton(document.getElementsByName("locationid"));
		var place=document.getElementById("place").value;
		var pnumber=document.getElementById("pnumber").value;
		var address=document.getElementById("address").value;
		var phomepage=document.getElementById("phomepage").value;
		var information=document.getElementById("information").value;
		var traffic=document.getElementById("traffic").value;
		var map=document.getElementById("map").value;
		if(place==""){
			alert("여행지를 입력하세요!");
			document.getElementById("place").focus();
			return;
		}else if(pnumber==""){
			alert("전화번호를 입력하세요!");
			document.getElementById("pnumber").focus();
			return;
		}else if(address==""){
			alert("주소를 입력하세요!");
			document.getElementById("address").focus();
			return;
		}else if(phomepage=="http://"){
			alert("홈페이지를 입력하세요!");
			document.getElementById("phomepage").focus();
			return;
		}else if(information==""){
			alert("소개를 입력하세요!");
			document.getElementById("information").focus();
			return;
		}else if(traffic==""){
			alert("교통을 입력하세요!");
			document.getElementById("traffic").focus();
			return;
		}else if(map==""){
			alert("약도를 입력하세요!");
			document.getElementById("map").focus();
			return;
		}
		xhr.send("mode=inputTravelInfo&place="+place+"&locationid="+locationid
				+"&pnumber="+pnumber+"&address="+address+"&phomepage="+phomepage
				+"&information="+information+"&traffic="+traffic+"&map="+map);
	}
	function callbackInputTravelInfo(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				alert(xhr.responseText);
				inputSelectZone();
				clearInputTravelInfo();
			}
		}
	}
	//여행지 정보 입력 후 창 지워주기
	function clearInputTravelInfo(){
		clearRadioButton(document.getElementsByName("locationid"));
		document.getElementById("place").value="";
		document.getElementById("pnumber").value="";
		document.getElementById("address").value="";
		document.getElementById("phomepage").value="http://";
		document.getElementById("information").value="";
		document.getElementById("traffic").value="";
		document.getElementById("map").value="";
	}
	/////////////////////////////////////////////////////////
	
	// 2.여행지 이미지 입력
	function startInputPlaceImage(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackInputPlaceImage; 
		xhr.open("POST","/wetogether/travelinfo_input.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");	
		var place=checkedRadioButton(document.getElementsByName("place"));
		var gallery=document.getElementById("gallery").value;
		if(gallery==""){
			alert("이미지를 입력하세요!");
			document.getElementById("place").focus();
			return;
		}
		xhr.send("mode=inputPlaceImage&place="+place+"&gallery="+gallery);
	}
	function callbackInputPlaceImage(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				alert(xhr.responseText);
				inputSelectPlace();
				clearInputPlaceImage();
			}
		}
	}
	// 이미지 값 입력 후 창 지워주기
	function clearInputPlaceImage(){
		document.getElementById("gallery").value="";
	}
	/////////////////////////////////////////////////
	
	// 3. 숙박 정보 입력
	function startInputPlaceVacancy(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackInputPlaceVacancy; 
		xhr.open("POST","/wetogether/travelinfo_input.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");
		var stay=document.getElementById("stay").value;
		var place=checkedRadioButton(document.getElementsByName("place"));
		var vnumber=document.getElementById("vnumber").value;
		var vhomepage=document.getElementById("vhomepage").value;
		if(stay==""){
			alert("숙박시설명을 입력하세요!");
			document.getElementById("stay").focus();
			return;
		}else if(vnumber==""){
			alert("전화번호를 입력하세요!");
			document.getElementById("vnumber").focus();
			return;
		}else if(vhomepage=="http://"){
			alert("홈페이지를 입력하세요!");
			document.getElementById("vhomepage").focus();
			return;
		}
		xhr.send("mode=inputPlaceVacancy&stay="+stay+"&place="+place+"&vnumber="+vnumber
				+"&vhomepage="+vhomepage);
	}
	function callbackInputPlaceVacancy(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				alert(xhr.responseText);
				inputSelectPlace();
				clearInputPlaceVacancy();
			}
		}
	}
	// 숙박정보 입력 후 창 지워주기
	function clearInputPlaceVacancy(){
		document.getElementById("stay").value="";
		document.getElementById("vnumber").value="";
		document.getElementById("vhomepage").value="http://";
	}
	/////////////////////////////////////////////////////////
	
	// 4. 맛집 정보 입력
	function startInputPlaceFr(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackInputPlaceFr; 
		xhr.open("POST","/wetogether/travelinfo_input.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");
		var restaurant=document.getElementById("restaurant").value;
		var place=checkedRadioButton(document.getElementsByName("place"));
		var fnumber=document.getElementById("fnumber").value;
		var fhomepage=document.getElementById("fhomepage").value;
		if(restaurant==""){
			alert("맛집명을 입력하세요!");
			document.getElementById("restaurant").focus();
			return;
		}else if(fnumber==""){
			alert("전화번호를 입력하세요!");
			document.getElementById("fnumber").focus();
			return;
		}else if(fhomepage=="http://"){
			alert("홈페이지를 입력하세요!");
			document.getElementById("fhomepage").focus();
			return;
		}
		xhr.send("mode=inputPlaceFr&restaurant="+restaurant+"&place="+place+"&fnumber="+fnumber
				+"&fhomepage="+fhomepage);
	}
	function callbackInputPlaceFr(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				alert(xhr.responseText);
				inputSelectPlace();
				clearInputPlaceFr();
			}
		}
	}
	// 맛집정보 입력 후 창 지워주기
	function clearInputPlaceFr(){
		document.getElementById("restaurant").value="";
		document.getElementById("fnumber").value="";
		document.getElementById("fhomepage").value="http://";
	}
	//////////////////////////////////////////////////////
	
	// 5. 공지사항 입력
	function startInputPlaceNotice(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackInputPlaceNotice; 
		xhr.open("POST","/wetogether/travelinfo_input.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");
		var place=checkedRadioButton(document.getElementsByName("place"));
		var notice=document.getElementById("notice").value;
		if(notice==""){
			alert("공지를 입력하세요!");
			document.getElementById("notice").focus();
			return;
		}
		xhr.send("mode=inputPlaceNotice&place="+place+"&notice="+notice);
	}
	function callbackInputPlaceNotice(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				alert(xhr.responseText);
				clearInputPlaceNotice();
			}
		}
	}
	// 공지사항 입력 후 창 지워주기
	function clearInputPlaceNotice(){
		clearRadioButton(document.getElementsByName("locationid"));
		document.getElementById("placelist").innerHTML="";
		document.getElementById("notice").value="";
	}
	///////////////////////////////////////////////////
	
	// 6. 댓글 입력
	function startInputPlaceComment(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackInputPlaceComment; 
		xhr.open("POST","/wetogether/travelinfo_input.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");	
		var place=document.getElementById("place").value;
		var memberid=document.getElementById("memberId").value;
		var content=document.getElementById("content").value;
		var recommend=document.getElementById("recommend").value;
		if(recommend==""){
			alert("'추천' 혹은 '추천안함' 버튼을 눌러주세요!");
			return;
		}else if(content==""){
			alert("내용을 입력하세요!");
			document.getElementById("content").focus();
			return;
		}
		xhr.send("mode=inputPlaceComment&place="+place+"&content="+content
				+"&recommend="+recommend+"&memberid="+memberid);
	}
	function callbackInputPlaceComment(){		
		if(xhr.readyState==4){		
			if(xhr.status==200){
				alert(xhr.responseText);
				clearInputPlaceComment();
				var locationId=document.getElementById("locationId").value;
				var place=document.getElementById("place").value;
				location.href="/wetogether/travelinfo_output.do?mode=outputTravelInfo&place="
					+place+"&locationId="+locationId;
			}
		}
	}
	// 댓글 입력 후 창 지워주기
	function clearInputPlaceComment(){
		document.getElementById("content").value="";
	}
	//////////////////////////////////////////////////
	
	// 해당 지역의 여행지 리스트 값 가져오기
	function inputSelectZone(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackInputSelectZone; 
		xhr.open("POST","/wetogether/travelinfo_input.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");	
		var locationid=checkedRadioButton(document.getElementsByName("locationid"));
		var change=document.getElementById("change").value;
		xhr.send("mode=findPlaceList&locationid="+locationid+"&change="+change);
	}
	function callbackInputSelectZone(){
		if(xhr.readyState==4){		
			if(xhr.status==200){
				var result=xhr.responseText;
				var jsonData=eval('('+result+')');
				if(jsonData.Emessage==null){
					// 여행지 입력창인지 유무 확인
					if(jsonData.change=="info"){
						document.getElementById("infoValue").innerHTML="존재하는 여행지 리스트 : "+jsonData.info;
					}else{
						document.getElementById("placelist").innerHTML=createRadioButton(jsonData.info);
					}
				}else{
					// 여행지 입력창인지 유무 확인
					if(jsonData.change=="info"){ 
						document.getElementById("infoValue").innerHTML=jsonData.Emessage;
					}else{
						document.getElementById("placelist").innerHTML=jsonData.Emessage;
					}
				}
				if(jsonData.change=="img"){
					document.getElementById("galleryinfo").innerHTML="";
				}else if(jsonData.change=="vacancy"){
					document.getElementById("vacancyinfo").innerHTML="";
				}else if(jsonData.change=="fr"){
					document.getElementById("frinfo").innerHTML="";
				}
			}
		}
	}
	// 선택한 여행지의 중복 체크
	function inputSelectPlace(){
		createXMLHttpRequest();		
		xhr.onreadystatechange=callbackInputSeletePlace; 
		xhr.open("POST","/wetogether/travelinfo_input.do",true);		
		xhr.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded;charset=UTF-8");	
		var place=checkedRadioButton(document.getElementsByName("place"));
		var change=document.getElementById("change").value;
		xhr.send("mode=getTravelInfoValue&place="+place+"&change="+change);
	}
	function callbackInputSeletePlace(){
		if(xhr.readyState==4){		
			if(xhr.status==200){
				var jsonData=eval('('+xhr.responseText+')');
				if(jsonData.change=="image"){ // 이미지 확인
					document.getElementById("galleryinfo").innerHTML=jsonData.isinputinfo;
				}else if(jsonData.change=="vacancy"){ // 숙박시설 확인
					document.getElementById("vacancyinfo").innerHTML=jsonData.isinputinfo;
				}else if(jsonData.change=="fr"){ // 맛집 확인
					document.getElementById("frinfo").innerHTML=jsonData.isinputinfo;
				}else if(jsonData.change=="notice"){ // 공지 확인
					if(jsonData.flag){ // 공지있으면 창 지워주기
						alert(jsonData.isinputinfo);
						clearRadioButton(document.getElementsByName("locationid"));
						document.getElementById("placelist").innerHTML="";
					}else{ // 공지 없으면 포커스 맞춰주기
						alert(jsonData.isinputinfo);
						document.getElementById("notice").focus();
					}
				}
			}
		}
	}
	/////////////////////////////////////////
	
	// 선택된 라디오버튼 값을 리턴
	function checkedRadioButton(data){
		var dataValue;
		for(i=0; i<data.length; i++){
			if(data[i].checked){
				dataValue=data[i].value;
				break;
			}
		}
		return dataValue;
	}
	// 라디오버튼 선택 해제
	function clearRadioButton(data){
		for(i=0; i<data.length; i++){
			data[i].checked=false;
		}
	}
	// 리스트의 값 만큼 라디오버튼 생성
	function createRadioButton(obj){
		var str="";
		for(i=0; i<obj.length; i++){
			str+="<input type='radio' name='place' value="+obj[i]+" onchange='inputSelectPlace()'></input>"+obj[i];
		}
		return str;
	}
	///////////////////////////////////////////
	
	// 댓글에서 추천하면 값을 1로 넣어주기
	function recommendTrue(){
		alert("'추천' 버튼을 누르셨습니다!");
		document.getElementById("recommend").value=1;
	}
	// 댓글에서 추천 안하면 값을 0으로 넣어주기
	function recommendFalse(){
		alert("'추천안함' 버튼을 누르셨습니다!");
		document.getElementById("recommend").value=0;
	}
	